﻿using System;
using System.Web.UI;
using viForum.Classes;
using viForum.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_viForum_BreadCrumb_BreadCrumb : WebModule
{
    private const string ForumLink = "<a href=\"{0}\" title=\"{1}\">{2}</a> ";
    /*****************************************************************************************/
    // Forum URL = Forum.aspx
    /*****************************************************************************************/
    /*****************************************************************************************/
    // forum URL = Forum/ForumId/ForumTitle .aspx
    /*****************************************************************************************/
    /*****************************************************************************************/
    // thread URL = Forum/Thread/ThreadId/ThreadTitle .aspx
    /*****************************************************************************************/
            
    protected override void WebModuleRender(HtmlTextWriter output)
    {        
        string qsForumId = Request.QueryString["ForumId"];
        string qsThreadId = Request.QueryString["ThreadId"];        
        if (!string.IsNullOrEmpty(qsForumId))
        {
            int forumid = 0;
            if (Int32.TryParse(qsForumId, out forumid))
            {
                Forum forum = ForumController.GetForum(forumid);
                if (forum != null)
                {
                    string data = "<h3 class=\"forum-BreadCrumb\">";
                    // Forum URL = Forum.aspx
                    string url = WebRoots.GetWebRoot + BaseController.viForumUrlSeparator + ".aspx";
                    data += string.Format(ForumLink, url, "Forum", "Forum");

                    // forum URL = Forum/ForumId/ForumTitle .aspx                    
                    data += " | " + string.Format(ForumLink, forum.Url, forum.Title, forum.Title);

                    int threadid = 0;
                    if (Int32.TryParse(qsThreadId, out threadid))
                    {
                        Thread thread = ThreadController.GetThread(threadid);
                        if (thread != null)
                        {
                            // thread URL = Forum/Thread/ThreadId/ThreadTitle .aspx                                                    
                            data += " | " + thread.Title;
                        }
                    }
                    if (!string.IsNullOrEmpty(data))
                    {
                        data += "</h3>";
                        output.Write(data); 
                    }
                }
            }            
        }        
    }
}
